package com.cloudeggtech.granite.chat.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.cloudeggtech.granite.chat.R;

/**
 * Created by david on 17/10/26.
 */

public class SplashActivity extends Activity {

    private static final String TAG = "SplashActivity";
    private static final int REQUEST_PHONE_PERMISSIONS = 0;
    private static final int LOGIN_RESULT_CODE = 100;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isLogin()) {
                    navToHome();
                } else {
                    navToLogin();
                }
            }
        }, 1000);
    }

    private boolean isLogin() {
        return false;
    }

    private void init() {

    }

    public void navToHome() {
        Log.d(TAG, "to home");
        Intent i = new Intent(this, HomeActivity.class);
        startActivity(i);
        finish();
    }

    public void navToLogin() {
        Log.d(TAG, "to login");
        Intent i = new Intent(this, LoginActivity.class);
        startActivity(i);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "request code:" + requestCode);
        if (requestCode == LOGIN_RESULT_CODE) {
            if (resultCode == RESULT_OK) {
                navToHome();
            } else {

            }
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PHONE_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    init();
                } else {
                    Toast.makeText(this, getString(R.string.need_permission), Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
